package com.sunmouse.toolkit.widget.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.sunmouse.toolkit.R;

/**
 * Created by leiyun on 2018/4/12.
 */
public class BasicView extends View {

    private Paint mPaint;
    private int mBorderWidth;
    private int mBorderColor;

    private float maxSize;

    public BasicView(Context context) {
        super(context);
    }

    public BasicView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.BasicView);

        mBorderWidth = arr.getDimensionPixelSize(R.styleable.BasicView_bvBorderWidth, 1);
        mBorderColor = arr.getColor(R.styleable.BasicView_bvBorderColor, Color.GRAY);

        arr.recycle();//don`t forget to do

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setColor(mBorderColor);
        mPaint.setStrokeWidth(mBorderWidth);
        mPaint.setStyle(Paint.Style.STROKE);


    }


    /**
     * onSizeChanged() is called when your view is first assigned a size, and again if the size
     * of your view changes for any reason.
     * Calculate positions, dimensions, and any other values related to your view's size in
     * onSizeChanged(), instead of recalculating them every time you draw.
     * In the PieChart example, onSizeChanged() is where the PieChart view calculates the
     * bounding rectangle of the pie chart and the relative position of the text label and
     * other visual elements.
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        int resW = getPaddingLeft() + getPaddingRight();
        int resH = getPaddingTop() + getPaddingBottom();

        int remW = w - resW;
        int remH = h - resH;

        maxSize = Math.min(remW, remH);

    }

    /**
     * If you need finer control over your view's layout parameters, implement onMeasure().
     * This method's parameters are View.MeasureSpec values that tell you how big your view's
     * parent wants your view to be, and whether that size is a hard maximum or just a suggestion.
     * As an optimization, these values are stored as packed integers, and you use the static
     * methods of View.MeasureSpec to unpack the information stored in each integer.
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
        int w = resolveSizeAndState(minw, widthMeasureSpec, 1);

        setMeasuredDimension(w, heightMeasureSpec);//after calculating,don`t forget to set these values
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, maxSize / 2, mPaint);
    }


}
